<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * gpt请求日志
 *
 * admin-GptRequestLog
 *
 * @Apidoc\Title("gpt请求日志")
 * @Apidoc\Group("api")
 */
class GptRequestLog extends BaseController
{
    /**
     * 新增
     *
     * admin-GptRequestLog-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\GptRequestLog::create($data['gpt_request_log']);
            return success();
        } else {
            $options = [];
            $gpt_request_log__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['gpt_request_log__member_id_options'] = $gpt_request_log__member_id_option;
            $gpt_request_log__gpt_token_id_option = \app\common\model\GptToken::column('name as name, id as id');
            $options['gpt_request_log__gpt_token_id_options'] = $gpt_request_log__gpt_token_id_option;
            $gpt_request_log__article_id_option = \app\common\model\Article::column('name as name, id as id');
            $options['gpt_request_log__article_id_options'] = $gpt_request_log__article_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-GptRequestLog-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\GptRequestLog::update($data['gpt_request_log']);
            return success();
        } else {
            $data = \app\common\model\GptRequestLog::find(input('gpt_request_log__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['gpt_request_log__' . $key] = $val;
            }
            $r['gpt_request_log__article_id'] = \app\common\model\Article::where('id', $r['gpt_request_log__article_id'])->find()->getData()['name'];
            View::assign('r', $r);
            $options = [];
            $gpt_request_log__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['gpt_request_log__member_id_options'] = $gpt_request_log__member_id_option;
            $gpt_request_log__gpt_token_id_option = \app\common\model\GptToken::column('name as name, id as id');
            $options['gpt_request_log__gpt_token_id_options'] = $gpt_request_log__gpt_token_id_option;
            $gpt_request_log__article_id_option = \app\common\model\Article::column('name as name, id as id');
            $options['gpt_request_log__article_id_options'] = $gpt_request_log__article_id_option;

            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-GptRequestLog-delete
     */
    public function delete()
    {
        \app\common\model\GptRequestLog::destroy(input('gpt_request_log__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-GptRequestLog-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_gpt_request_log');
        $query->join('db_member', 'db_member.id = db_gpt_request_log.member_id');
        $query->join('db_article', 'db_article.id = db_gpt_request_log.article_id');
        //$query->join('db_gpt_token', 'db_gpt_token.id = db_gpt_request_log.gpt_token_id');
        if (input('gpt_request_log__create_time_start', '') || input('gpt_request_log__create_time_end', '')) {
            $gpt_request_log__create_time_start = input('gpt_request_log__create_time_start', '', 'search_in');
            $gpt_request_log__create_time_end = input('gpt_request_log__create_time_end', '', 'search_in');
            $gpt_request_log__create_time_start = $gpt_request_log__create_time_start ? strtotime($gpt_request_log__create_time_start) : 0;
            $gpt_request_log__create_time_end = $gpt_request_log__create_time_end ? strtotime($gpt_request_log__create_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_gpt_request_log.create_time', 'between', [$gpt_request_log__create_time_start, $gpt_request_log__create_time_end]);
        }
        if (input('gpt_request_log__article_id', '') !== '') {
            $query->where('db_gpt_request_log.article_id', '=', input('gpt_request_log__article_id', '', 'search_in'));
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
//        if (input('gpt_token__id', '') !== '') {
//            $query->where('db_gpt_token.id', '=', input('gpt_token__id', '', 'search_in'));
//        }
        $query->where('db_gpt_request_log.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        //$query->where('db_gpt_token.delete_time', '=', null);
        $query->field(['db_gpt_request_log.id' => 'gpt_request_log__id']);
        $query->field(['db_gpt_request_log.create_time' => 'gpt_request_log__create_time']);
        $query->withAttr('gpt_request_log__create_time', function ($value, $data) {
            return date('Y-m-d H:i:s', $value);
        });
        $query->field(['db_gpt_request_log.prompt_tokens' => 'gpt_request_log__prompt_tokens']);
        $query->field(['db_gpt_request_log.completion_tokens' => 'gpt_request_log__completion_tokens']);
        $query->field(['db_gpt_request_log.total_tokens' => 'gpt_request_log__total_tokens']);
        $query->field(['db_gpt_request_log.reason_for_error' => 'gpt_request_log__reason_for_error']);
        $query->field(['db_gpt_request_log.article_id' => 'gpt_request_log__article_id']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_article.name' => 'article__name']);
        //$query->field(['db_gpt_token.name' => 'gpt_token__name']);
        $query->order('db_gpt_request_log.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        $gpt_token__id_option = \app\common\model\GptToken::column('name as name, id as id');
        $options['gpt_token__id_options'] = $gpt_token__id_option;
        View::assign($options);
        return View::fetch();
    }
}